package com.Offer;

import java.util.Arrays;

/*
    面试题33：二叉搜索树的后序遍历序列
    题目描述：输入一个整数数组，判断该数组是不是某二叉搜索树的后序遍历的结 果。如果是则输出 Yes,否则输出 No。假设输入的数组的任意两个数字都互不相 同。
 */
public class demo33 {

    public static void main(String[] args) {

    }

    public static boolean VerifySquenceOfBST(int[] sequence){
        if(sequence == null || sequence.length == 0){
            return false;
        }
        int start = 0;
        int rootIndex = sequence.length-1;
        for(int i = 0;i < rootIndex;i++){
            if(sequence[i] < sequence[rootIndex]){
                start++;
            }
        }
        if(start == 0){
            VerifySquenceOfBST(Arrays.copyOfRange(sequence,0,rootIndex));
            return true;
        }

        for(int i  = start;i < rootIndex;i++){
            if(sequence[i] <= sequence[rootIndex]){
                return false;
            }
        }
        VerifySquenceOfBST(Arrays.copyOfRange(sequence,0,start));
        VerifySquenceOfBST(Arrays.copyOfRange(sequence,start,rootIndex));
        return true;
    }

}
